﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using IService.Utility;
using System.Configuration;
using IService.DataDLL.Utility;

namespace IService
{
	public partial class Register : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{

		}

		//开始注册
		protected void btnRegist_Click(object sender, EventArgs e)
		{
			Model.User user = new Model.User();
			user.UserName = txtUserName.Text.Trim();
			user.Password = Md5Helper.md5(txtPassword.Text.Trim(), 32);
			user.Email = txtEmail.Text.Trim();
			user.RealUserName = txtRealUserName.Text.Trim();
			user.Phone = txtPhone.Text.Trim();
			user.CompanyName = txtCompanyName.Text.Trim();
			user.CompanyAddress = txtCompanyAddress.Text.Trim();
			user.CompanyPhone = txtCompanyPhone.Text.Trim();
            user.RegistDateTime = DateTime.Now;
            user.UpdateDateTime = DateTime.Now;
            user.UserRole = "";
            user.EmployeesTotal = 0;
            user.IndustryClassification = "";
            user.Supply = "";
            user.Purchase = "";
            user.CompanyProfile = "";
            user.UserMobile = "";
            user.UserFax = "";
            user.UserAddress = "";
            user.UserLoginDate = DateTime.Now;

			if (CheckUserNameExists())
			{
				WebMessageBox.Show(this.Page, "会员账号已经存在，请重新输入！");
				return;
			}
			else if (CheckEmailExists())
			{
				WebMessageBox.Show(this.Page, "E-Mail已经存在，请重新输入！");
				return;
			}
			else
			{
				if (Business.DTVBusiness.UserManager.InsertUser(user))
				{
                    string username = txtUserName.Text.Trim();
                    string password = Md5Helper.md5(txtPassword.Text.Trim(), 32);  //DEncrypt.Encrypt(txtPassword.Text.Trim());
                    Model.User userFromSQL = Business.DTVBusiness.UserManager.Login(username, password);
					//发送邮件
					//MailUtil.SendEmail(user.Email, "欢迎来到IService", CommUtil.CreateHtmlBody("Welcome.htm", user.UserName, ConfigurationManager.AppSettings["WebServerPath"].ToString()));

                    SessionState.CurrentUser = userFromSQL;
                        
                    WebMessageBox.ShowAndRedirect(this.Page, "注册成功！", "User_center/index.aspx");
                   
				}
				else
				{
					WebMessageBox.Show(this.Page, "注册失败，请重新再试！");
				}
			}

		}

		/// <summary>
		/// 注册时，检查用户的Email是否存在
		/// </summary>
		/// <returns></returns>
		public bool CheckEmailExists()
		{
			Model.User user = Business.DTVBusiness.UserManager.GetUserByEmail(txtEmail.Text.Trim());
			if (user.ID == 0)
			{
				return false;
			}
			else
			{
				return true;
			}
		}

		/// <summary>
		/// 注册时，检查用户的会员账号是否存在
		/// </summary>
		/// <returns></returns>
		public bool CheckUserNameExists()
		{
			Model.User user = Business.DTVBusiness.UserManager.GetUserByUserName(txtUserName.Text.Trim());
			if (user.ID == 0)
			{
				return false;
			}
			else
			{
				return true;
			}
		}
	}
}